﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jy123.DBUtility;

namespace Jy123.BLL.Venues
{
    public class VModule : Jy123.BLL.Public.Module<Jy123.Model.Venues_Main>
    {
        /// <summary>
        /// 组件升级(条件)
        /// </summary>
        /// <param name="id">组件ID</param>
        /// <param name="ckey">条件组</param>
        /// <param name="cvalue">条件值组</param>
        /// <returns>成功失败</returns>
        public override bool LevelUpByCondition(int id, List<string> cKey, List<int> cValue, decimal money)
        {
            Jy123.BLL.Venues_Main BLLVenuesMain = new Jy123.BLL.Venues_Main();
            Jy123.Model.Venues_Main venues = BLLVenuesMain.GetModel(id);
            Jy123.BLL.User_Main BLLUserMain = new Jy123.BLL.User_Main();
            Jy123.Model.User_Main user = BLLUserMain.GetModel(venues.FUser.Value);
            if (user.Cash >= money)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT ");
                int count = cKey.Count;
                for (int i = 0; i < count - 1; i++)
                {
                    strSql.Append(cKey[i].ToString());
                    strSql.Append(",");
                }
                strSql.Append(cKey[count - 1]);
                strSql.AppendFormat(" FROM Venues_Main WHERE ID = {0}", id);
                DataSet dsResult = DbHelperSQL.Query(strSql.ToString());//获取组件对应字段的值
                if (dsResult != null)
                {
                    DataRow drResult = dsResult.Tables[0].Rows[0];
                    for (int j = 0; j < cValue.Count; j++)
                    {
                        string keyName = cKey[j].ToString();
                        int iResult = int.Parse(drResult[keyName].ToString());
                        int iValue = int.Parse(cValue[j].ToString());
                        if (iResult < iValue)
                        {
                            return false;//比较组件对应各字段是否达到升级条件
                        }
                    }
                }
                else
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 升级赛场，扣除相应费用
        /// </summary>
        /// <param name="venues">赛场</param>
        /// <param name="money">升级费用</param>
        /// <param name="user">用户</param>
        /// <returns>是否成功</returns>
        public bool UpgradeModule(Jy123.Model.Venues_Main venues, decimal money, Jy123.Model.User_Main user)
        {
            try
            {
                Module_Main BLLModule = new Module_Main();
                Venues_Main BLLVenues = new Venues_Main();
                User_Main BLLUser = new User_Main();
                Public_Level BLLLevel = new Public_Level();
                Model.Public_Level cLevel = BLLLevel.GetModel(venues.FLevel.Value);
                int nLNumber = cLevel.LevelNumber.Value + 1;
                string strWhere = string.Format("FJType = {0} AND LevelNumber = {1}", cLevel.FJType.Value, nLNumber.ToString());
                Model.Public_Level nLevel = BLLLevel.GetModelList(strWhere)[0];
                venues.FLevel = nLevel.ID;
                BLLVenues.Update(venues);
                user.Cash = Convert.ToInt32(user.Cash.Value - money);
                BLLUser.Update(user);
                Model.Module_Main module = BLLModule.GetModel(venues.ID);
                BLL.Public.State state = new Jy123.BLL.Public.State();
                state.CreateModuleState(user, module, Convert.ToInt32(Public.EnumConfig.StateActionType.UpdateModule));
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 返回好友某个类型赛场组件列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="JType">赛场类型</param>
        /// <returns>该类型赛场对象组</returns>
        public List<Jy123.Model.Venues_Main> FriendsVenues(int userId, int JType, bool status)
        {
            Venues_Main BLLVenuesMain = new Venues_Main();
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat("FJType = {0} ", JType);
            strWhere.Append("AND FUser IN (");
            strWhere.Append("SELECT FFriend FROM User_Friend ");
            strWhere.AppendFormat("WHERE FUser = {0}) ", userId);
            if (status)
            {
                strWhere.Append("AND ID IN (");
                strWhere.AppendFormat("SELECT ID FROM Module_Main WHERE (MStatus = 0 OR MStatus = 1 OR MStatus = 2))");//表示组件为空闲或者等待或者比赛状态
            }
            strWhere.Append(" ORDER BY VStatus ASC");
            List<Jy123.Model.Venues_Main> resultList = BLLVenuesMain.GetModelList(strWhere.ToString());
            return resultList;
        }

        /// <summary>
        /// 返回好友某个类型正在比赛的赛场组件列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="JType">赛场类型</param>
        /// <returns>该类型赛场对象组</returns>
        public List<Jy123.Model.Venues_Main> FriendsRaceVenues(int userId, int JType)
        {
            Venues_Main BLLVenuesMain = new Venues_Main();
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat("FJType = {0} ", JType);
            strWhere.Append("AND FUser IN (");
            strWhere.Append("SELECT FFriend FROM User_Friend ");
            strWhere.AppendFormat("WHERE FUser = {0}) ", userId);
            strWhere.Append("AND ID IN (");
            strWhere.AppendFormat("SELECT ID FROM Module_Main WHERE  MStatus = 2)");//表示组件为比赛状态
            List<Jy123.Model.Venues_Main> resultList = BLLVenuesMain.GetModelList(strWhere.ToString());
            return resultList;
        }

        /// <summary>
        /// 返回组件对象
        /// </summary>
        /// <param name="id">组件ID</param>
        /// <returns>该类型组件对象组</returns>
        public override List<Jy123.Model.Venues_Main> FriendsModule(int id) { throw new NotImplementedException(); }
        /// <summary>
        /// 返回组件列表
        /// </summary>
        /// <param name="field">列名</param>
        /// <param name="count">总数</param>
        /// <param name="order">排序类型([T]asc[F]Desc)</param>
        /// <returns>该类型组件对象组</returns>
        public override List<Jy123.Model.Venues_Main> OrderModule(string field, int count, bool order) { throw new NotImplementedException(); }
        /// <summary>
        /// 返回好友组件列表
        /// </summary>
        /// <param name="id">组件ID</param>
        /// <param name="field">列名</param>
        /// <param name="count">总数</param>
        /// <param name="order">排序类型([T]asc[F]Desc)</param>
        /// <returns>该类型组件对象组</returns>
        public override List<Jy123.Model.Venues_Main> OrderMFriend(int id, string field, int count, bool order) { throw new NotImplementedException(); }
    }
}
